import { Column, Entity, PrimaryColumn } from "typeorm";
import dayjs from "dayjs";
import { DATE_TIME_FORMATTER } from "@chulei-ui/utils";
import type { PlayerPlanet } from "../../plants/entity/PlayerPlanet";
import type { AuthUser, Token } from "@scvzerng/esi-163";
export class Role {
  id: number;
  name: string;
  planets?: PlayerPlanet[];
  constructor(role: AuthRole) {
    this.id = role.id;
    this.name = role.name;
  }
}
@Entity("auth_role")
export class AuthRole {
  /**
   * 角色ID
   */
  @PrimaryColumn({ type: "integer" })
  id!: number;
  /**
   * 角色名称
   */
  @Column("text")
  name!: string;
  /**
   * 续期令牌
   */
  @Column("text")
  refresh_token!: string;
  /**
   * 访问令牌
   */
  @Column("text")
  access_token!: string;
  /**
   * 过期时间戳
   */
  @Column("text")
  exp!: string;

  static from(token: Token, authUser: AuthUser) {
    const authRole = new AuthRole();
    authRole.id = authUser.id;
    authRole.name = authUser.name;
    authRole.access_token = token.access_token;
    authRole.refresh_token = token.refresh_token;
    //过去时间 20分钟有效实际只使用18分钟即认为失效
    authRole.exp = dayjs().add(token.expires_in, "seconds").subtract(2, "minutes").format(DATE_TIME_FORMATTER);
    return authRole;
  }
}
